package com.kin207.netty.server;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.eventbus.EventBus;
import com.kin207.netty.handle.IMessageHandleAdapter;
import com.kin207.netty.listener.eventbus.CreatedListenerEvent;
import com.kin207.netty.listener.eventbus.DestroyedListenerEvent;
import com.kin207.netty.msg.IMessageParse;
import com.kin207.netty.session.SessionManager;

import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;

public abstract class ServerAbs implements IServer{
	final static Logger logger = LoggerFactory.getLogger(ServerAbs.class);
	
	protected ServerManager serverManager;
	
	protected SessionManager sessionManager;
	
	protected IMessageHandleAdapter messageHandleAdapter;
	
	protected IMessageParse messageParse;
	
	protected int port;
	
	protected ChannelInitializer channelInitializer;
	
	protected ChannelFuture channelFuture;
	
	protected EventBus eventBus;
	
	protected String name;
	
	public ServerAbs(int port){
		this.port = port;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public ChannelInitializer getChannelInitializer() {
		return channelInitializer;
	}
	
	public void setChannelInitializer(ChannelInitializer channelInitializer){
		this.channelInitializer = channelInitializer;
	}

	public SessionManager getSessionManager() {
		return sessionManager;
	}

	public void setSessionManager(SessionManager sessionManager) {
		this.sessionManager = sessionManager;
	}

	public EventBus getEventBus() {
		return eventBus;
	}

	public void setEventBus(EventBus eventBus) {
		this.eventBus = eventBus;
	}

	public void postCreatedEvent(IServer o) {
		if(getEventBus()!=null){
			logger.debug("event bus post DestroyedListenerEvent<IServer>:{}", o.getServerType());
			getEventBus().post(new CreatedListenerEvent<IServer>(o));
		}
	}

	public void postDestroyedEvent(IServer o) {
		if(getEventBus()!=null){
			logger.debug("event bus post DestroyedListenerEvent<IServer>:{}", o.getServerType());
			getEventBus().post(new DestroyedListenerEvent<IServer>(o));
		}
	}

	public IMessageHandleAdapter getMessageHandleAdapter() {
		return messageHandleAdapter;
	}

	public void setMessageHandleAdapter(IMessageHandleAdapter messageHandleAdapter) {
		this.messageHandleAdapter = messageHandleAdapter;
	}

	public IMessageParse getMessageParse() {
		return messageParse;
	}

	public void setMessageParse(IMessageParse messageParse) {
		this.messageParse = messageParse;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public ServerManager getServerManager() {
		return serverManager;
	}

	public void setServerManager(ServerManager serverManager) {
		this.serverManager = serverManager;
	}
	
}
